package net.medplus.social.comm;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import java.util.ArrayList;

/**
 * Description:Android6.0权限工具,子类需要实现{@link PermissionTools#processForApplyGranted()}和{@link PermissionTools#processForApplyDenied(Activity, String[], int)}
 * 分别去处理用户授予权限后和用户拒绝授予权限后的结果
 *
 * @author: Xian XueLiang
 * @date: 2016/6/12
 */
public abstract class PermissionTools {
    private static final String TAG = PermissionTools.class.getSimpleName();
    /** 请求码，request code */
    private int mPermissionRequestCode = -1;

    /**
     * Android6.0动态权限申请
     * @param activity The target activity.
     * @param permissions 需要申请的权限集
     * @param requestCode 在Activity的回調方法{@link Activity#onRequestPermissionsResult(int, String[], int[])}中接收(權限申請結果)。
     */
    public final void permissionRequestForAndroid6_0(@NonNull Activity activity, @NonNull  String[] permissions, int requestCode){
        if (permissions.length>0) {
            mPermissionRequestCode = requestCode;
            ArrayList<String> needReqPermissions = new ArrayList<>();
            for (String p : permissions) {
                if (ContextCompat.checkSelfPermission(activity.getApplicationContext(), p) == PackageManager.PERMISSION_DENIED) {
                    needReqPermissions.add(p);
                }
            }
            if (!needReqPermissions.isEmpty()) {
                String[] tmp = new String[needReqPermissions.size()];
                needReqPermissions.toArray(tmp);
                ActivityCompat.requestPermissions(activity,tmp,requestCode);
            }else
                Log.d(TAG, "Need not to apply for All Permissions");
        }else{
            Log.d(TAG, "Permissions length must > 0!!");
        }
    }

    /**
     * 处理权限申请结果，在调用前需要先调用{@link #permissionRequestForAndroid6_0(Activity, String[], int)}方法进行权限请求。
     * 本方法应该在Activity的回调方法{@link Activity#onRequestPermissionsResult(int, String[], int[])}中调用。
     * @param activity The target activity.
     * @param requestCode 请求码 {@link PermissionTools #permissionRequestForAndroid6_0(Context, String[], int)}
     * @param permissions 申请的权限
     * @param grantResults 与permissions中--对应的权限申请结果，授予=0{@link PackageManager#PERMISSION_GRANTED},拒绝=-1{@link PackageManager#PERMISSION_DENIED}
     */
    public final void processResult(Activity activity,int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode==mPermissionRequestCode) {
            ArrayList<String> deniedPermissions = new ArrayList<>();
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i]== PackageManager.PERMISSION_DENIED) {
                    deniedPermissions.add(permissions[i]);
                }
            }
            if (!deniedPermissions.isEmpty()) {
                //处理用户拒绝的权限
                String[] tmp = new String[deniedPermissions.size()];
                deniedPermissions.toArray(tmp);
                processForApplyDenied(activity,tmp,requestCode);
            }else{
                //所有权限授予
                processForApplyGranted();
            }
        }
    }

    /**
     * 处理用户拒绝授予的权限，子类需要实现
     * @param activity The target activity.
     * @param permissions 用户拒绝授予的权限
     */
    public abstract void processForApplyDenied(Activity activity,String[] permissions,int requestCode);

    /**
     * 处理请求权限被用户全部授予的情况，子类需要实现
     */
    public abstract void processForApplyGranted();
}
